package com.dd.cloud.user.service.admin.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.AdminConstants;
import com.dd.cloud.user.entity.admin.ContactUs;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.user.mapper.admin.ContactUsMapper;
import com.dd.cloud.user.req.admin.UpdateContactUsReq;
import com.dd.cloud.user.res.admin.ContactUsRes;
import com.dd.cloud.user.service.admin.IContactUsService;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.enums.ZkExceptionEnum;
import com.dd.cloud.common.exception.ZekeException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 联系我们功能表 服务实现类
 * </p>
 *
 * @author czg
 * @since 2022-07-12
 */
@Service
public class ContactUsServiceImpl extends ServiceImpl<ContactUsMapper, ContactUs> implements IContactUsService {

    /**
     * 修改联系我们
     * @param req
     */
    @Override
    public void updateContactUs(UpdateContactUsReq req) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.ADMIN).get();
        ContactUs contactUs = getById(req.getId());
        if (contactUs == null || contactUs.getStatus() == AdminConstants.ContactUsStatus.DEL){
            throw new ZekeException(400, "数据不存在");
        }
        BeanUtils.copyProperties(req, contactUs);
        contactUs.setOperId(loginInfo.getId());
        contactUs.setCreateDate(LocalDateTime.now());
        boolean flag = updateById(contactUs);
        if (!flag){
            throw new ZekeException(ZkExceptionEnum.UPDATE_ERROR);
        }
    }

    /**
     * 查询联系我们数据
     * @return
     */
    @Override
    public List<ContactUsRes> getContactUs() {
        List<ContactUsRes> list = getBaseMapper().getContactUs();
        return list;
    }
}
